import java.util.Scanner;

public class TicketService {
    private Scanner scanner;

    public TicketService(Scanner scanner) {
        this.scanner = scanner;
    }

    // 显示座位图
    private void displaySeats(boolean[] seats) {
        System.out.println("\n座位图 (O: 可售, X: 已售):");
        System.out.println("-------------------------");

        // 假设座位是10列布局
        int cols = 10;
        int rows = (seats.length + cols - 1) / cols;

        for (int i = 0; i < rows; i++) {
            System.out.print("第" + (i + 1) + "排: ");
            for (int j = 0; j < cols; j++) {
                int index = i * cols + j;
                if (index < seats.length) {
                    System.out.print(seats[index] ? "X " : "O ");
                }
            }
            System.out.println();
        }
        System.out.println("-------------------------");
        System.out.println("总座位数: " + seats.length + ", 可售座位: " + getAvailableSeatsCount(seats));
    }

    // 计算可售座位数
    private int getAvailableSeatsCount(boolean[] seats) {
        int count = 0;
        for (boolean seat : seats) {
            if (!seat) count++;
        }
        return count;
    }

    // 售票
    public boolean sellTicket() {
        System.out.print("请输入要购票的场次ID: ");
        String scheduleId = scanner.nextLine();

        Schedule schedule = CinemaData.findScheduleById(scheduleId);
        if (schedule == null) {
            System.out.println("场次不存在！");
            return false;
        }

        Movie movie = CinemaData.findMovieById(schedule.getMovieId());
        if (movie == null) {
            System.out.println("影片信息不存在！");
            return false;
        }

        System.out.println("影片信息: " + movie.getName() + " (" + movie.getDuration() + ")");
        System.out.println("放映信息: " + schedule.getHall() + " " + schedule.getDate() + " " + schedule.getTime());
        System.out.println("票价: " + movie.getPrice() + "元");

        // 显示座位图
        displaySeats(schedule.getSeats());

        if (getAvailableSeatsCount(schedule.getSeats()) == 0) {
            System.out.println("该场次已售罄！");
            return false;
        }

        System.out.print("请输入要购买的座位号 (1-" + schedule.getSeats().length + "): ");
        int seatNum = 0;
        try {
            seatNum = Integer.parseInt(scanner.nextLine());
            if (seatNum < 1 || seatNum > schedule.getSeats().length) {
                System.out.println("座位号输入错误！");
                return false;
            }
        } catch (NumberFormatException e) {
            System.out.println("输入错误！");
            return false;
        }

        // 座位索引从0开始
        int seatIndex = seatNum - 1;

        if (schedule.isSeatSold(seatIndex)) {
            System.out.println("该座位已售出！");
            return false;
        }

        // 执行售票
        if (schedule.sellSeat(seatIndex)) {
            System.out.println("购票成功！座位号: " + seatNum +
                    ", 总价: " + movie.getPrice() + "元");
            System.out.println("请保管好您的票根，祝您观影愉快！");
            return true;
        }

        System.out.println("购票失败！");
        return false;
    }

    // 退票
    public boolean refundTicket() {
        System.out.print("请输入要退票的场次ID: ");
        String scheduleId = scanner.nextLine();

        Schedule schedule = CinemaData.findScheduleById(scheduleId);
        if (schedule == null) {
            System.out.println("场次不存在！");
            return false;
        }

        Movie movie = CinemaData.findMovieById(schedule.getMovieId());
        if (movie == null) {
            System.out.println("影片信息不存在！");
            return false;
        }

        System.out.print("请输入要退的座位号: ");
        int seatNum = 0;
        try {
            seatNum = Integer.parseInt(scanner.nextLine());
            if (seatNum < 1 || seatNum > schedule.getSeats().length) {
                System.out.println("座位号输入错误！");
                return false;
            }
        } catch (NumberFormatException e) {
            System.out.println("输入错误！");
            return false;
        }

        // 座位索引从0开始
        int seatIndex = seatNum - 1;

        if (!schedule.isSeatSold(seatIndex)) {
            System.out.println("该座位未售出，无法退票！");
            return false;
        }

        // 执行退票
        if (schedule.refundSeat(seatIndex)) {
            System.out.println("退票成功！座位号: " + seatNum +
                    ", 退款金额: " + movie.getPrice() + "元");
            return true;
        }

        System.out.println("退票失败！");
        return false;
    }
}
